/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DatabaseConnection;

import Entities.Employee;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author serhat

 */
public class EmployeeDbConnection extends Connector {

    public EmployeeDbConnection() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
    }

    private String buildInsertQuery(Employee employee) {
        String query = String.format(
                "INSERT INTO Personel "
                + "VALUES('%d', '%s', '%s', '%s',"
                + " '%s', '%s', '%f', '%d', "
                + "'%d', '%s')",
                employee.getTcNo(),
                employee.getName(),
                employee.getLastName(),
                employee.getBirthdate(),
                employee.getMobilePhone(),
                employee.getPhone(),
                employee.getSalary(),
                employee.getHour(),
                employee.getState(),
                employee.getPosition());
        return query;
    }
    
    private String buildSelectQuery(Employee employee){
        String query = "SELECT * FROM Personel WHERE";
        
        if(employee.getTcNo() != 0){
            query += " TcKimlik = '" + employee.getTcNo() + "'";  
        }
        if(!employee.getName().equals("")){
            if (query.endsWith("'"))
                query += " AND ";
            query += " Ad='" + employee.getName() +"'";
        }
        if(!employee.getLastName().equals("")){
            if (query.endsWith("'"))
                query += " AND ";
            query += " Soyad='" + employee.getLastName() +"'";
        }
        System.out.println(query);
        return query;
    }
    
    private String buildUpdateQuery(Employee employee){
         String query = String.format(
                "UPDATE Personel "
                + "SET " 
                + "Ad='%s', Soyad='%s', DogumTarihi='%s', CepTelefonu='%s',"
                + "SabitTelefon='%s', Maas='%f', HaftalikCalismaSaati='%d', "
                + "Durum='%d', Görev='%s'"
                + " Where TcKimlik='%d'",
                employee.getName(),
                employee.getLastName(),
                employee.getBirthdate(),
                employee.getMobilePhone(),
                employee.getPhone(),
                employee.getSalary(),
                employee.getHour(),
                employee.getState(),
                employee.getPosition(),
                employee.getTcNo());
         System.out.println(query);
        return query;
    }
    public boolean insert(Employee employee) {

        try {
            String query = buildInsertQuery(employee);
            executeUpdate(query);
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDbConnection.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;

    }
    
    public ResultSet select(Employee employee){
        try {
            String query = buildSelectQuery(employee);
            ResultSet rs = executeQuery(query);
            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDbConnection.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public boolean update(Employee employee){
        try {
            String query = buildUpdateQuery(employee);
            executeUpdate(query);
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDbConnection.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
}
